package views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import models.Paciente;
import models.Turno;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import controllers.Cancelar_Turno_Controller;
import controllers.MyPacientesTableModel;

import controllers.MyTurnosTableModel;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Cancelar_Turno extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JButton btnCancelar;
	private JButton btnConfirmarCancelacion;
	private JTable tablaTurnosPaciente;
	private JScrollPane scrollTablaTurnosPaciente;
	private JLabel lblTurnos;
	private Pantalla_Turnos parent;
	private MyTurnosTableModel modelTurnosPaciente;
	private Actions l= new Actions();
	private Cancelar_Turno_Controller controller= Cancelar_Turno_Controller .getInstancia();
	/**
	* Auto-generated main method to display this JFrame
	 * @param parent 
	*/

	public Cancelar_Turno(Pantalla_Turnos parent) {
		super();
		this.parent=parent;
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			this.setTitle("cancelar turno");
			this.setPreferredSize(new java.awt.Dimension(874, 426));
			{
				btnConfirmarCancelacion = new JButton();
				getContentPane().add(btnConfirmarCancelacion, new AnchorConstraint(110, 919, 182, 740, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnConfirmarCancelacion.setText("Confirmar Cancelacion");
				btnConfirmarCancelacion.setPreferredSize(new java.awt.Dimension(155, 28));
				btnConfirmarCancelacion.addActionListener(l);
			}
			{
				scrollTablaTurnosPaciente = new JScrollPane();
				getContentPane().add(scrollTablaTurnosPaciente, new AnchorConstraint(213, 920, 700, 37, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				scrollTablaTurnosPaciente.setPreferredSize(new java.awt.Dimension(765, 191));
				{
					tablaTurnosPaciente = new JTable();
					scrollTablaTurnosPaciente.setViewportView(tablaTurnosPaciente);
				}
			}
			{
				lblTurnos = new JLabel();
				getContentPane().add(lblTurnos, new AnchorConstraint(62, 465, 154, 24, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				lblTurnos.setText("Seleccione turno a cancelar");
				lblTurnos.setPreferredSize(new java.awt.Dimension(382, 36));
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar, new AnchorConstraint(845, 968, 947, 805, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				btnCancelar.setText("Volver");
				btnCancelar.setPreferredSize(new java.awt.Dimension(141, 40));
				btnCancelar.addActionListener(l);
			}
			pack();
			this.setSize(874, 426);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setTableModel(Vector<Turno> turnos)
	{
		modelTurnosPaciente = new MyTurnosTableModel(turnos);
		tablaTurnosPaciente.setModel(modelTurnosPaciente);
	}
	
	public void cleanScreen()
	{
		
	}
	public class Actions implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Object source = e.getSource();
			if(source == btnCancelar)
			{
				parent.setEnabled(true);
				cleanScreen();
				setVisible(false);
			}
			else if (source == btnConfirmarCancelacion)
			{
				if(tablaTurnosPaciente.getSelectedRowCount() == 1)
				{
					Turno cancelado= modelTurnosPaciente.getTurnoAt(tablaTurnosPaciente.getSelectedRow());
					if(controller.cancelarTurno(cancelado))
						JOptionPane.showMessageDialog(null, "Turno cancelado correctamente");
					else
						JOptionPane.showMessageDialog(null, "Error al cancelar el turno");
				}
				else {
					JOptionPane.showMessageDialog( null, "Debe seleccionar un turno");
				}
			}
		}
	}

}
